﻿
--岗位异动申请
CREATE proc [dbo].[proc_Employee_PostMoveApply]
(
	@eid int,
	@ACompanyId int,
	@ADepartmentId nvarchar(50),
	@APostCategoryId int,
	@APostId int,
	@AGradeId int,
	@AState int,
	@AZhiWu nvarchar(50),
	@Reason text,
	@Proposer nvarchar(50),
	@id int,
	@kbn int,
	@execdate DATETIME,
	@XZJB NVARCHAR(50),
	@IsCore bit
)
As
BEGIN

    Declare @PostMove1_Id int --判断是否有未执行的公司和部门异动
	Set @PostMove1_Id = Null
	Select Top 1 @PostMove1_Id=id From Employee_CompanyMove Where Audit=0 And Eid = @eid And AuditPerson Is Null
	If @PostMove1_Id Is Not Null	--该人有尚未审批的申请公司异动，退出
	Begin
		Select -2 As ReturnValue
		Return
	End

	Declare @PostMove_Id int
	Set @PostMove_Id = Null
	--一级未审、二级未审、未执行
	Select Top 1 @PostMove_Id=id From Employee_PostMove Where ((FirstAudit=0 And FirstAuditPerson Is Null)
	Or (FirstAudit=1 And SecondAudit=0 And SecondAuditPerson Is Null)
	Or (SecondAudit=1 And IsExec=0)) And Eid = @eid
	If @PostMove_Id Is Not Null	--该人有尚未审批或未执行的申请，退出
	Begin
		Select -1 As ReturnValue
		Return
	End

declare @BCompanyId int,@BDepartmentId nvarchar (50),@BPostId int,@BGradeId int,@BZhiWu nvarchar(50),@EName nvarchar(50),@BPostCategoryId INT,@BXZJB NVARCHAR(50)
declare @BCompanyName nvarchar(200),@ACompanyName nvarchar(200),@BDepartmentName nvarchar(200),@ADepartmentName nvarchar(200),@BPostName nvarchar(50),@BGradeName nvarchar(50),@APostName nvarchar(50),@AGradeName nvarchar(50)

set @BCompanyId = (select CompanyId from employee where id = @eid)
set @BDepartmentId = (select DepartmentId from employee where id = @eid)
set @BPostCategoryId = (select PostCategory from employee where id = @eid)
set @BPostId = (select PostId from employee where id = @eid)
set @BGradeId = (select GradeId from employee where id = @eid)
set @BZhiWu=(select ZhiWu from employee where id = @eid)
set @EName=(select EName from employee where id = @eid)
SET @BXZJB = (select XZJB from employee where id = @eid)

---------
select @BCompanyName = CompanyName from company where id=@BCompanyId
select @BDepartmentName = Dname from department where id = @BDepartmentId
select @ACompanyName= CompanyName from company where id=@ACompanyId
select @ADepartmentName=Dname from department where id = @ADepartmentId
select @BPostName= PostName from Post  where id=@BPostId
select @BGradeName=Title from Grade  where id=@BGradeId
select @APostName= PostName from Post  where id=@APostId
select @AGradeName=Title from Grade  where id=@AGradeId

	If @EName Is Null Or @BCompanyId Is Null Or @BCompanyId Is Null
	Begin
		Select 0 As ReturnValue
		Return
	End

	Insert Into Employee_PostMove
	(
		eid,
		EName,
		BCompanyId,
		BDepartmentId,
		BPostCategoryId,
		BPostId,
		BGradeId,
		BZhiWu,
		ACompanyId,
		ADepartmentId,
		APostCategoryId,
		APostId,
		AGradeId,
		AZhiWu,
		AState,
		Reason,
		Proposer,
		ApplionTime,
		execdate,
		AXZJB,
		BXZJB,
		BCompanyName,
		BDepartmentName,
		ACompanyName,
		ADepartmentName,
		BPostName,
		BGradeName,
		APostName,
		AGradeName,
		IsCore
	)
	Values
	(
		@eid,
		@EName,
		@BCompanyId,
		@BDepartmentId,
		@BPostCategoryId,
		@BPostId,
		@BGradeId,
		@BZhiWu,
		@ACompanyId,
		@ADepartmentId,
		@APostCategoryId,
		@APostId,
		@AGradeId,
		@AZhiWu,
		@AState,
		@Reason,
		@Proposer,
		getdate(),
		--DATEADD(mm, DATEDIFF(mm,0,@execdate), 0) --每月的1日
		@execdate,
		@XZJB,
		@BXZJB,
		@BCompanyName,
		@BDepartmentName,
		@ACompanyName,
		@ADepartmentName,
		@BPostName,
		@BGradeName,
		@APostName,
		@AGradeName,
		@IsCore
	)

	Select @@ROWCOUNT As ReturnValue
	Return
End